package com.tcl.utilcode;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;

/**
 * @Author Mack
 * @Date 2020/9/24
 */
public class SystemUtils {

    public static String getForegroundPackageName(Context context){
        try{
            ActivityManager am = (ActivityManager)context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
            ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
            return cn.getPackageName();
        }catch (Exception e){

        }
        return "";
    }

    public static String getForegroundClassName(Context context) {
        try {
            ActivityManager am = (ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
            ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
            return cn.getClassName();
        } catch (Exception e) {

        }
        return "";
    }

    public static ComponentName getForegroundApp(Context context){
        try {
            ActivityManager am = (ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
            ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
            return cn;
        } catch (Exception e) {

        }
        return null;
    }
}
